package tt.love.hw;


import tt.love.hw.email.EmailTemplate;


public class EmailTemplateFactory {
	private static final EmailTemplateFactory instance = new EmailTemplateFactory();

	private EmailTemplateFactory() {

	}

	public static EmailTemplateFactory get() {
		return instance;
	}

	public EmailTemplate createEmailTemplate(int templateType, String[][] args) {
		EmailTemplate emailTemplate = new EmailTemplate();
		switch (templateType) {
		case 1:
			emailTemplate.setSubject("New application request");
			emailTemplate
					.setMsgBody("Dear ${name},\n\t your mate just request new application, please check in time.\nRegards");
			break;
		case 2:
			emailTemplate.setSubject("Your application has been approved");
			emailTemplate
					.setMsgBody("Dear ${name},\n\t your application has been approved. Enjoy it.\nRegards");
			break;
		case 3:
			emailTemplate.setSubject("Your application has been rejected");
			emailTemplate
					.setMsgBody("Dear ${name},\n\t your application has been rejected. Sorry for it.\nRegards");
		case 4:
			emailTemplate.setSubject("Your application has been cancelled");
			emailTemplate
					.setMsgBody("Dear ${name},\n\t your application has been cancelled. Please check with your mate.\nRegards");
		case 5:
			emailTemplate.setSubject("Her/His application has been cancelled");
			emailTemplate
					.setMsgBody("Dear ${name},\n\t your mate`s application has been cancelled. Please check with your mate.\nRegards");
			break;
		default:

		}
		String msgBody = emailTemplate.getMsgBody();
		for (String[] arg : args) {
			String argname = arg[0];
			String argvalue = arg[1];
			int argStartIndex = msgBody.indexOf("${" + argname + "}");
			int argEndIndex = argStartIndex+2+argname.length()+1;
			msgBody = msgBody.substring(0, argStartIndex) + argvalue
					+ msgBody.substring(argEndIndex);
		}
		emailTemplate.setMsgBody(msgBody);
		return emailTemplate;
	}
	

}
